<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>新增内容</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/js/layui/css/layui.css" media="all">

</head>
<body>
<div style="width:80%;margin:15px auto;overflow:auto;" id="aaa">
    <form class="layui-form" action="">
        <input type="hidden" class="input-text" id="mId" name="mId">
        <div class="layui-form-item" id="box1">
            <label class="layui-form-label" id="tip-title">图片标题</label>
            <div class="layui-input-block">
                <input type="text" name="title" id="title" required  lay-verify="required"  placeholder="请输入图片标题" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-upload">
            <button type="button" class="layui-btn" id="test" style="display: inline;float: left;">上传图片</button>
            <div class="layui-upload-list" style="display: inline;margin-left: 20px;">
                <img class="layui-upload-img" name="picture" id="picture" style="width: 200px;height: 150px">
                <p id="demoText"></p>
            </div>
            <input type="hidden" class="input-text" id="logo" name="logo">
            <i class="layui-btn layui-btn-danger layui-btn-xs imgtips" style="margin-left: 160px;margin-top: 10px;display: none;"></i>
        </div>

        <div class="layui-form-item" style="margin-top: 20px;" id="box2">
            <label class="layui-form-label" id="hint-describe">文本描述</label>
            <div class="layui-input-block">
                <script id="editor_tai" type="textain" style="height: 200px;width:98%;margin: auto"></script>
            </div>
        </div>

        <div class="layui-form-item" style="margin-top: 20px">
            <div class="layui-input-block">
                <button class="layui-btn" id="addImgText" lay-submit lay-filter="addImgText">立即添加</button>
                <button type="reset" id="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>
<script src="/js/layui/layui.js"></script>
<script src="/js/layui/layui.all.js"></script>
<script src="/js/ueditor/1.4.3/ueditor.config.js" type="text/javascript" charset="utf-8"></script>
<script src="/js/ueditor/1.4.3/ueditor.all.js" type="text/javascript" charset="utf-8"></script>
<script src="/admin/js/token.js"></script>
<script src="../js/verify.js"></script>
<script>

    //url 传参
    function GetQueryString(name){
        var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
        var a = decodeURI(decodeURI(window.location.search));
        var r = a.substr(1).match(reg);//search,查询？后面的参数，并匹配正则
        if(r!=null)return  unescape(r[2]); return null;
    }

    window.onload = function(){
        $("#mId").val(GetQueryString("mId"));
    }

    var ue = UE.getEditor('editor_tai');

    layui.use(['form' , 'jquery' , 'upload'], function(){
        var form = layui.form;
        var $ = layui.jquery;
        var upload = layui.upload;
        var mId = GetQueryString("mId");
        if(mId=='1499199424666939393'){ // 如果是首页的“在线解答”模块
            $("#logo").val("abc.jpg");  // 设置一个假图片，这样就不要上传图片了，骗过后台把复制图片到upload这个目录，从而不会报错
            $(".layui-upload").hide();
        }
        if(mId=='1495599488959807489'){     // 如果是专业价值
            let s= showImgTips(mId);
            $(".imgtips").html(s);
            $(".imgtips").show()
        }
        if(mId=='1498106055912022018'){ // 如果是就业服务,隐藏服务标题
            $("#box1").hide();
            $("#title").attr("lay-verify",'');
            $("#title").prop("required",false);
        }
        if(mId=='1498105399138541569'){// 如果是就业服务,隐藏图片上传
            $(".layui-upload").hide();
        }
        if(mId=='1495599488959807489'){ // 如果是专业价值，就隐藏富文本编辑器
            $("#box2").hide();
        }
        //添加操作
        form.on('submit(addImgText)',function(data){
            var picture = $("#logo").val();
            var title = $("#title").val();
            var description = ue.getContent();
            data.field.mId = $("#mId").val();
           title =  replaceReg(title);  // 替换
            if((title==null || title=="") && (description==null || description=="")){
                layer.msg('请填写标题或描述！');
            } else if ( (picture==null || picture=="") && mId!="1498105399138541569") {
                layer.msg('请上传图片！');
            }else {
                let url="";
                if(picture=="abc.jpg")  // 如果是假图片，没有上传新图片
                    url = picture;
                else
                    url = "/tmp/"+picture;
                var data = {"content":url,"title":title,"description":description};
                parent.getTableData(data);
                var index = parent.layer.getFrameIndex(window.name);
                parent.layer.close(index);//关闭弹出层
            }
            return false;
        });

        //logo上传
        var uploadInst = upload.render({
            elem: '#test'
            , url: '/upload' //改成您自己的上传接口
            , before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#picture').attr('src', result); //图片链接（base64）
                });
            }
            , done: function (res) {
                //判断上传是否成功
                if (res.code == 0) {
                    $("#logo").val(res.data);
                    return layer.msg('上传成功！');
                }else{
                    return layer.msg('上传失败！');
                }
            }
            , error: function () {
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInst.upload();
                });
            }
        });

        //修改提示文字的内容
        let hint1 = showTitle(mId);
        let hint2 = showDesri(mId);
        $("#tip-title").html(hint1);
        $("#hint-describe").html(hint2);

    });
</script>
</body>
</html>